//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sinosoft.hydra.um.dao.impl;

import com.sinosoft.hydra.um.dao.SysLogInfoDao;
import com.sinosoft.hydra.um.domain.model.LogInfo;
import cn.com.sinux.spring.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysLogInfoDaoImpl implements SysLogInfoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public SysLogInfoDaoImpl() {
    }

    public Map<String, Object> queryCountForReport(Date startDate, Date endDate) {
        String sql_info = "SELECT L.level_string AS LEVELSTRING, COUNT(1) AS COUNT FROM logging_event L  WHERE L.timestmp >= ? AND L.timestmp <= ?  GROUP BY level_string order by levelString asc ";
        List<LogInfo> list = this.jdbcTemplate.query(sql_info, new Object[]{startDate, endDate}, new RowMapper<LogInfo>() {

            public LogInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
                LogInfo logInfo = new LogInfo();
                logInfo.setLevelType(rs.getString("LEVELSTRING"));
                logInfo.setCount(rs.getInt("COUNT"));
                return logInfo;
            }
        });
        Map<String, Object> countMap = new HashMap<String, Object>();
        Iterator i$ = list.iterator();

        while (i$.hasNext()) {
            LogInfo map = (LogInfo) i$.next();
            String key = map.getLevelType();
            if (key.toUpperCase().equals("INFO")) {
                countMap.put("INFO", map.getCount());
            } else if (key.toUpperCase().equals("DEBUG")) {
                countMap.put("DEBUG", map.getCount());
            } else if (key.toUpperCase().equals("ERROR")) {
                countMap.put("ERROR", map.getCount());
            } else if (key.toUpperCase().equals("TRACE")) {
                countMap.put("TRACE", map.getCount());
            } else if (key.toUpperCase().equals("WARN")) {
                countMap.put("WARN", map.getCount());
            }
        }

        return countMap;
    }

    private List<Integer> buildDate(List<String> dateRanges, List<LogInfo> listInfo, String level){
        List<Integer> result = new ArrayList<Integer>();
        for(String range : dateRanges){
            int count = 0;
            for(LogInfo logInfo : listInfo){
                if(level.equals(logInfo.getLevelType()) && range.equals(logInfo.getTimeKey())){
                    count = logInfo.getCount();
                    break;
                }
            }
            result.add(count);
        }
        return result;
    }

    public Map<String, Object> queryLogInfoList(Date startDate, Date endDate) {
        List<String> dateRanges = DateUtils.getDateRange(startDate, endDate);
        String sql = "SELECT LEVEL_STRING, date_format(TIMESTMP, '%Y-%m-%d') as TIMEKEY,  count(1) as COUNT FROM logging_event " +
                "where TIMESTMP between ? AND ? GROUP BY LEVEL_STRING, date_format(TIMESTMP, '%Y-%m-%d')";
        RowMapper<LogInfo> rowMapper = new RowMapper<LogInfo>() {

            public LogInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
                LogInfo logInfo = new LogInfo();
                logInfo.setTimeKey(rs.getString("TIMEKEY"));
                logInfo.setLevelType(rs.getString("LEVEL_STRING"));
                logInfo.setCount(rs.getInt("COUNT"));
                return logInfo;
            }
        };
        List<LogInfo> listInfo = this.jdbcTemplate.query(sql, new Object[]{startDate, endDate}, rowMapper);
        Map<String, Object> countMap = new HashMap<String, Object>();
        countMap.put("xAxis", dateRanges);
        countMap.put("series_info", buildDate(dateRanges, listInfo, "INFO"));
        countMap.put("series_debug", buildDate(dateRanges, listInfo, "DEBUG"));
        countMap.put("series_trace", buildDate(dateRanges, listInfo, "TRACE"));
        countMap.put("series_warn", buildDate(dateRanges, listInfo, "WARN"));
        countMap.put("series_error", buildDate(dateRanges, listInfo, "ERROR"));
        return countMap;
    }

    public Map<String, Object> queryLogInfoListByYearMonth(Date startDate, Date endDate) {
        List<String> dateRanges = DateUtils.getMonthRange(startDate, endDate);
        String sql = "SELECT LEVEL_STRING, date_format(TIMESTMP, '%Y-%m') as TIMEKEY,  count(1) as COUNT FROM logging_event " +
                "where TIMESTMP between ? AND ? GROUP BY LEVEL_STRING, date_format(TIMESTMP, '%Y-%m')";
        RowMapper<LogInfo> rowMapper = new RowMapper<LogInfo>() {

            public LogInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
                LogInfo logInfo = new LogInfo();
                logInfo.setTimeKey(rs.getString("TIMEKEY"));
                logInfo.setLevelType(rs.getString("LEVEL_STRING"));
                logInfo.setCount(rs.getInt("COUNT"));
                return logInfo;
            }
        };
        List<LogInfo> listInfo = this.jdbcTemplate.query(sql, new Object[]{startDate, endDate}, rowMapper);
        Map<String, Object> countMap = new HashMap<String, Object>();
        countMap.put("xAxis", dateRanges);
        countMap.put("series_info", buildDate(dateRanges, listInfo, "INFO"));
        countMap.put("series_debug", buildDate(dateRanges, listInfo, "DEBUG"));
        countMap.put("series_trace", buildDate(dateRanges, listInfo, "TRACE"));
        countMap.put("series_warn", buildDate(dateRanges, listInfo, "WARN"));
        countMap.put("series_error", buildDate(dateRanges, listInfo, "ERROR"));
        return countMap;
    }

    public Map<String, Object> queryLogInfoListByYear(Date startDate, Date endDate) {
        List<String> dateRanges = DateUtils.getYearRange(startDate, endDate);
        String sql = "SELECT LEVEL_STRING, date_format(TIMESTMP, '%Y') as TIMEKEY,  count(1) as COUNT FROM logging_event " +
                "where TIMESTMP between ? AND ? GROUP BY LEVEL_STRING, date_format(TIMESTMP, '%Y')";
        RowMapper<LogInfo> rowMapper = new RowMapper<LogInfo>() {

            public LogInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
                LogInfo logInfo = new LogInfo();
                logInfo.setTimeKey(rs.getString("TIMEKEY"));
                logInfo.setLevelType(rs.getString("LEVEL_STRING"));
                logInfo.setCount(rs.getInt("COUNT"));
                return logInfo;
            }
        };
        List<LogInfo> listInfo = this.jdbcTemplate.query(sql, new Object[]{startDate, endDate}, rowMapper);
        Map<String, Object> countMap = new HashMap<String, Object>();
        countMap.put("xAxis", dateRanges);
        countMap.put("series_info", buildDate(dateRanges, listInfo, "INFO"));
        countMap.put("series_debug", buildDate(dateRanges, listInfo, "DEBUG"));
        countMap.put("series_trace", buildDate(dateRanges, listInfo, "TRACE"));
        countMap.put("series_warn", buildDate(dateRanges, listInfo, "WARN"));
        countMap.put("series_error", buildDate(dateRanges, listInfo, "ERROR"));
        return countMap;
    }
}
